﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RollBackLib.src.core;

namespace RollBackLib.src.ui.Dialogs.Preferences
{
    public partial class EmailDetailsDialog : UserControl
    {
        public EmailDetailsDialog()
        {
            InitializeComponent();
            this.m_emailIdGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.m_emailIdGridView_CellFormatting);        
        }

        internal void populateDialog(EmailAndSmtpTable emailAndSmtpTable)
        {
            m_emailIdGridView.Rows.Clear();
            m_smtpHostsGridView.Rows.Clear();
            foreach (SMTPEntry smtpData in emailAndSmtpTable.m_smtpTable.m_smtpList)
            {
                if (smtpData.m_ServerName.Trim().Length > 0)
                {
                    DataGridViewRow item = new DataGridViewRow();
                    DataGridViewCell cell1 = new DataGridViewTextBoxCell();
                    DataGridViewCell cell2 = new DataGridViewTextBoxCell();
                    DataGridViewCheckBoxCell cell3 = new DataGridViewCheckBoxCell();

                    cell1.Value = smtpData.m_ServerName;
                    cell2.Value = smtpData.m_ServerPort;
                    cell3.Value = smtpData.m_useSSL.Equals("True", StringComparison.CurrentCultureIgnoreCase) ? true : false;

                    item.Cells.AddRange(new DataGridViewCell[] {
                        cell1,
                        cell2,
                        cell3,
                    });
                    m_smtpHostsGridView.Rows.Add(item);
                }
            }

            foreach (EmailEntry emailData in emailAndSmtpTable.m_emailTable.m_EmailList)
            {
                if (emailData.m_EmailId.Trim().Length > 0)
                {
                    DataGridViewRow item = new DataGridViewRow();
                    DataGridViewCell cell1 = new DataGridViewTextBoxCell();
                    DataGridViewTextBoxCell cell2 =  new DataGridViewTextBoxCell();
                    DataGridViewComboBoxCell cell3 = new DataGridViewComboBoxCell();

                    cell1.Value = emailData.m_EmailId;
                    cell2.Value = new PasswordText(emailData.m_Password);
                    
                    // TODO: Inefficient, the map gets created for every emailid.
                    Dictionary<string, int> map = refreshComboBox(cell3, emailAndSmtpTable.m_smtpTable.m_smtpList);
                    if (emailData.m_keyOfSmtpServerList != null && map.ContainsKey(emailData.m_keyOfSmtpServerList))
                    {
                        cell3.Value = emailData.m_keyOfSmtpServerList;
                    }

                    item.Cells.AddRange(new DataGridViewCell[] {
                    cell1,
                    cell2,
                    cell3,
                });
                    m_emailIdGridView.Rows.Add(item);
                }
            }
        }

        private Dictionary<string, int> refreshComboBox(DataGridViewComboBoxCell cell3, List<SMTPEntry> list)
        {
            // map of SMTP keys to its entry in the combobox item.
            Dictionary<string, int> map = new Dictionary<string, int>();
            cell3.Items.Clear();
            int i = 0;
            foreach (SMTPEntry e in list)
            {
                string k = e.getKey();
                if (k.Trim().Length > 0)
                {
                    map[k] = i;
                    cell3.Items.Add(k);
                }
                i++;
            }
            return map;
        }

        public EmailAndSmtpTable getEmailAndSmtpData()
        {
            EmailAndSmtpTable data = new EmailAndSmtpTable();
            {
                DataGridViewRow[] row = new DataGridViewRow[m_emailIdGridView.Rows.Count];
                m_emailIdGridView.Rows.CopyTo(row, 0);
                foreach (DataGridViewRow r in row)
                {
                    // make sure the data in the cell is valid.
                    if (r.Cells != null && r.Cells.Count >= 3)
                    {
                        string emailId = r.Cells[0].Value == null ? "" : r.Cells[0].Value.ToString();
                        string password = r.Cells[1].Value == null ? "" : r.Cells[1].Value.ToString();
                        string smtpKey = r.Cells[2].Value == null ? "" : r.Cells[2].Value.ToString();

                        if (emailId.Trim().Length > 0)
                        {
                            EmailEntry e = new EmailEntry(emailId, password, smtpKey);
                            data.addEmailEntry(e);
                        }
                    }
                }
            }

            {
                DataGridViewRow[] row2 = new DataGridViewRow[m_smtpHostsGridView.Rows.Count];
                m_smtpHostsGridView.Rows.CopyTo(row2, 0);
                foreach (DataGridViewRow r in row2)
                {
                    // make sure the data in the cell is valid.
                    if (r.Cells != null && r.Cells.Count >= 3)
                    {
                        string host = r.Cells[0].Value == null ? "" : r.Cells[0].Value.ToString();
                        string port = r.Cells[1].Value == null ? "" : r.Cells[1].Value.ToString();
                        string useSsl = r.Cells[2].Value == null ? "" : r.Cells[2].Value.ToString();

                        if (host.Trim().Length > 0)
                        {
                            SMTPEntry e = new SMTPEntry(host, port, useSsl);
                            data.addSMTPEntry(e);
                        }
                    }
                }
            }
            return data;
        }

        private void m_refreshButton_Click(object sender, EventArgs e)
        {
            refreshEmailAndSmtpDetails();
        }

        private void refreshEmailAndSmtpDetails()
        {
            EmailAndSmtpTable emailAndSmtpTable = getEmailAndSmtpData();
            populateDialog(emailAndSmtpTable);
        }

        private void m_emailIdGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (e.Value != null)
                {
                    String s = new String('*', e.Value.ToString().Length);
                    e.Value = s;
                }
            }
            else
            {
                
            }
        }
    }

    class PasswordText
    {
        public String m_data;
        public PasswordText(String s)
        {
            m_data = s;
        }
        public override string ToString()
        {
            return m_data;
        }
    }
}
